package com.nd.qa.test.common;

import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Calendar;

public class CommonLib {

	public static String getCurrentTime() {
        try {
            Calendar ca = Calendar.getInstance();
            int year = ca.get(Calendar.YEAR);
            int month = ca.get(Calendar.MONTH);
            int day = ca.get(Calendar.DATE);
            int minute = ca.get(Calendar.MINUTE);
            int hour = ca.get(Calendar.HOUR);
            int second = ca.get(Calendar.SECOND);
            String currentTime = (String.valueOf(year) + "-" + String.valueOf(month + 1) + "-"
                    + String.valueOf(day) + "-" + String.valueOf(hour) + "-"
                    + String.valueOf(minute) + "-" + String.valueOf(second));
            Log.i(NewSolo.Tag, "The current time is: " + currentTime);
            return currentTime;
        } catch (Exception e) {
            Log.e(NewSolo.Tag, "Exceptionet the current time ");
            return "00000000";
        }
    }
    
    public static String dateToStrLong(Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

    public static String getDifferenceTime(String lastTime,String latestTime){
    	SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date begin;
		try {
			begin = dfs.parse(lastTime);
			Date end = dfs.parse(latestTime);
			long between=(end.getTime()-begin.getTime())/1000;
			//除以1000是为了转换成秒
			  long day = between / (24 * 60 * 60 * 1000);
		      long hour = (between / (60 * 60 * 1000) - day * 24);
		      long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
		      long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		      long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
		                - min * 60 * 1000 - s * 1000);
		        System.out.println(day + "天" + hour + "小时" + min + "分" + s + "秒" + ms
		                + "毫秒");
		        StringBuffer difTime = new StringBuffer("");
		        if (day!=0) {
		        	difTime.append(day + "天");
				}
		        if (hour!=0) {
					difTime.append(hour + "小时");
				}
				if (min!=0) {
					difTime.append(min + "分");				
				}
				if (s!=0) {
					difTime.append(s + "秒");
				}
				if (ms!=0) {
					difTime.append(ms+ "毫秒");
				}
			 return difTime.toString();
		} catch (ParseException e) {
			e.printStackTrace();
			return "00000000";
		}
	}

}

	


